﻿using System;
using System.Threading;
using Styx.Common;
using Styx.CommonBot;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Action = Styx.TreeSharp.Action;

namespace TuanHA_Monk
{
    public partial class Classname
    {
        #region Mistweaver Rotation

        private Composite MistweaverRotation()
        {
            return new PrioritySelector(
                //Starting the movement right here
                //But first, facing target
                //new Decorator(
                //    ret =>
                //    !ValidUnit(UnitAttack) ||
                //    !IsEnemy(UnitAttack) ||
                //    !IsOverrideModeOn,
                //    new Action(delegate
                //                   {
                //                       WoWMovement.StopFace();
                //                       return RunStatus.Failure;
                //                   })
                //    ),
                //new Decorator(
                //    ret =>
                //    ValidUnit(UnitAttack) &&
                //    IsEnemy(UnitAttack) &&
                //    !Me.IsFacing(UnitAttack) &&
                //    UnitAttack.IsWithinMeleeRange &&
                //    UnitAttack.InLineOfSight &&
                //    !IsOverrideModeOn,
                //    new Action(delegate
                //                   {
                //                       UnitAttack.Face();
                //                       return RunStatus.Failure;
                //                   })
                //    ),
                //MovementMoveBehind(ret => UnitAttack),
                new Decorator(
                    ret =>
                    Me.Mounted,
                    new Action(delegate { return RunStatus.Success; })
                    ),
                //Hold Heal while channelling Spinning Crane Kick
                new Decorator(
                    ret =>
                    Me.IsCasting && Me.ChanneledCastingSpellId == 101546,
                    new Action(delegate { return RunStatus.Success; })
                    ),
                //new Decorator(
                //    ret =>
                //    Me.Mounted,
                //    new Action(delegate
                //                   {
                //                       return RunStatus.Success;
                //                   })
                //    ),
                //Heal Rotation Here
                // [Stance of the Wise Serpent when there's enough mana to heal]
                new Decorator(
                    ret =>
                    !Me.Mounted &&
                    !CastingorGCDL() &&
                    Me.CurrentMana > Me.MaxMana*0.15 &&
                    !Me.HasAura("Stance of the Wise Serpent"),
                    new Action(delegate { CastSpell("Stance of the Wise Serpent", Me); })
                    ),
                // [Stance of the Fierce Tiger when there's no mana to heal ]
                new Decorator(
                    ret =>
                    !Me.Mounted &&
                    !CastingorGCDL() &&
                    Me.CurrentMana < Me.MaxMana*0.05 &&
                    ValidUnit(UnitAttack) && IsEnemy(UnitAttack) &&
                    UnitAttack.IsWithinMeleeRange &&
                    (!HasTalent.Contains("Chi Wave") ||
                     HasTalent.Contains("Chi Wave") &&
                     SpellManager.Spells["Chi Wave"].CooldownTimeLeft.TotalSeconds < 4) &&
                    !Me.HasAura("Stance of the Fierce Tiger"),
                    new Action(delegate { CastSpell("Stance of the Fierce Tiger", Me); })
                    ),
                new Decorator(
                    ret =>
                    !Me.Mounted &&
                    THSettings.Instance.STouchofDeath &&
                    ValidUnit(UnitAttack) && IsEnemy(UnitAttack) &&
                    Me.IsFacing(UnitAttack) &&
                    !CastingorGCDL() && UnitAttack.IsWithinMeleeRange &&
                    (Me.CurrentChi >= 3 || HasGlyph.Contains("Glyph of Touch of Death")) &&
                    UnitAttack.CurrentHealth > Me.CurrentHealth - (Me.CurrentHealth*0.2) &&
                    SpellManager.HasSpell("Touch of Death") && Me.HasAura("Death Note"),
                    new Action(delegate
                                   {
                                       //Logging.Write(LogLevel.Diagnostic, "Call Touch of Death pls");
                                       if (!Me.IsFacing(UnitAttack))
                                       {
                                           UnitAttack.Face();
                                       }
                                       CastSpell("Touch of Death", UnitAttack);
                                   })
                    ),
                new Decorator(
                    ret =>
                    !Me.Mounted &&
                    THSettings.Instance.SSpearHandStrike &&
                    Me.CurrentMana > Me.MaxMana*0.015 &&
                    ValidUnit(UnitAttack) && IsEnemy(UnitAttack) &&
                    Me.IsFacing(UnitAttack) &&
                    UnitAttack.IsWithinMeleeRange &&
                    UnitAttack.IsCasting && UnitAttack.CanInterruptCurrentSpellCast &&
                    //Me.CurrentEnergy >= 30 &&
                    SpellManager.HasSpell("Spear Hand Strike") &&
                    SpellManager.Spells["Spear Hand Strike"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       //Logging.Write(LogLevel.Diagnostic, "Call Spear Hand Strike pls");
                                       if (!Me.IsFacing(UnitAttack))
                                       {
                                           UnitAttack.Face();
                                       }
                                       CastSpell("Spear Hand Strike", UnitAttack);
                                       return RunStatus.Failure;
                                   })
                    ),
                //Invoke Xuen, the White Tiger, use as a tank life saver
                //Need to recode for faster and shorter
                new Decorator(
                    ret =>
                    THSettings.Instance.SXuen &&
                    ValidUnit(UnitAttack) && IsEnemy(UnitAttack) &&
                    UnitAttack.IsWithinMeleeRange &&
                    Me.Combat &&
                    UnitAttack.CurrentHealth > Me.CurrentHealth - (Me.CurrentHealth*0.2) &&
                    (UnitAttack.IsPlayer || UnitAttack.MaxHealth > Me.MaxHealth*10) &&
                    UnitAttack.GotTarget && UnitAttack.CurrentTarget.HasAura("Vengeance") &&
                    UnitAttack.CurrentTarget.HealthPercent < THSettings.Instance.SUrgentHeal &&
                    SpellManager.HasSpell("Invoke Xuen, the White Tiger") &&
                    SpellManager.Spells["Invoke Xuen, the White Tiger"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       CastSpell("Invoke Xuen, the White Tiger", UnitAttack);
                                       return RunStatus.Failure;
                                   })),
                //Chi Torpedo Fun lol
                new Decorator(
                    ret =>
                    !Me.Mounted &&
                    //Fix Expel Harm cooldown till HonorBuddy do it
                    THSettings.Instance.SRoll &&
                    LastCastSpell != "Roll" &&
                    !Me.IsMoving &&
                    Me.Combat &&
                    HasTalent.Contains("Chi Torpedo") &&
                    GetUnitChiTorpedo() && UnitChiTorpedo.IsValid &&
                    Me.IsSafelyFacing(UnitChiTorpedo) &&
                    SpellManager.Spells["Roll"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       if (!Me.IsSafelyFacing(UnitAttack))
                                       {
                                           UnitAttack.Face();
                                       }
                                       Logging.Write(LogLevel.Diagnostic, "Call Chi Torpedo Please!!!");
                                       CastSpell("Roll", UnitChiTorpedo);
                                       return RunStatus.Failure;
                                   })
                    ),
                //Detox ASAP here
                new Decorator(
                    ret =>
                    Me.HasAura("Stance of the Wise Serpent") &&
                    Me.CurrentMana > Me.MaxMana*0.045 &&
                    UnitHeal != null && UnitHeal.IsValid && UnitHeal.Combat &&
                    UnitHeal.HealthPercent <= THSettings.Instance.SLifeCocoon &&
                    (UnitHeal.HasAura("Vengeance") && Me.CurrentMap.IsDungeon ||
                     UnitHeal.HasAura("Vengeance") && Me.CurrentMap.IsRaid ||
                     IsMeorFocus(UnitHeal)) &&
                    !CastingorGCDL() &&
                    SpellManager.Spells["Life Cocoon"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate { CastSpell("Life Cocoon", UnitHeal); })),
                new Decorator(
                    ret =>
                    //Prevent No Path bugs
                    //damn it, due to the mesh issue, gotta add an timer spam here
                    THSettings.Instance.SSummonJadeSerpentStatue &&
                    LastDropStatueAttempt.AddSeconds(10) < DateTime.Now &&
                    LastCastSpell != "Summon Jade Serpent Statue" &&
                    Me.HasAura("Stance of the Wise Serpent") &&
                    Me.CurrentMana > Me.MaxMana*0.09 &&
                    //Summon Jade Serpent Statue - Don't know why can't drop why moving, change logic
                    //Found problem, it's mesh, better drop on my location, no delay
                    !CastingorGCDL() &&
                    UnitHeal != null && UnitHeal.IsValid &&
                    UnitHeal.Combat &&
                    (!GetMyStatue() || GetMyStatue() &&
                     MyStatue.Location.Distance(UnitHeal.Location) > 40) &&
                    (CountFriendlyNearyNeedHeal(UnitHeal, 30, 100) > 3 ||
                     UnitHeal.HasAura("Vengeance") && (Me.CurrentMap.IsDungeon || Me.CurrentMap.IsRaid) ||
                     UnitHeal == Me) &&
                    SpellManager.HasSpell("Summon Jade Serpent Statue") &&
                    SpellManager.Spells["Summon Jade Serpent Statue"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       LastDropStatueAttempt = DateTime.Now;
                                       //Logging.Write(LogLevel.Diagnostic, "Call Spinning Crane Kick pls");
                                       CastSpell("Summon Jade Serpent Statue", Me);
                                       Thread.Sleep(TimeSpan.FromMilliseconds(100));
                                       ObjectManager.Update();
                                       SpellManager.ClickRemoteLocation(UnitHeal.Location);
                                       LastStatueLocation = UnitHeal.Location;
                                   })
                    ),
                new Decorator(
                    ret =>
                    //Fix Expel Harm cooldown till HonorBuddy do it
                    LastCastSpell != "Expel Harm" &&
                    !CastingorGCDL() &&
                    Me.HealthPercent < THSettings.Instance.SPriorityHeal &&
                    //Me.CurrentEnergy >= 40 && //Fix, Thank Abroseh
                    //Because it generate 2 chi 
                    Me.CurrentChi <= Me.MaxChi - 1 &&
                    SpellManager.HasSpell("Expel Harm") &&
                    SpellManager.Spells["Expel Harm"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       //Logging.Write(LogLevel.Diagnostic, "Call Expel Harm");
                                       CastSpell("Expel Harm", Me);
                                   })
                    ),
                new Decorator(
                    ret =>
                    Me.HasAura("Stance of the Wise Serpent") &&
                    //MyAuraStackCount("Mana Tea", Me) < 20 &&
                    Me.CurrentMana > Me.MaxMana*0.0458 &&
                    Me.CurrentChi < Me.MaxChi &&
                    !CastingorGCDL() &&
                    SpellManager.HasSpell("Renewing Mist") &&
                    SpellManager.Spells["Renewing Mist"].CooldownTimeLeft.TotalMilliseconds <= MyLatency &&
                    GetUnitRenewingMist() && UnitRenewingMist.IsValid,
                    new Action(delegate
                                   {
                                       //Logging.Write(LogLevel.Diagnostic, "Call Spinning Crane Kick pls");
                                       CastSpell("Renewing Mist", UnitRenewingMist);
                                   })
                    ),
                new Decorator(
                    ret =>
                    THSettings.Instance.SSpearHandStrike &&
                    ValidUnit(UnitAttack) && IsEnemy(UnitAttack) &&
                    UnitAttack.IsWithinMeleeRange &&
                    //use to interrupt or survive
                    (UnitAttack.IsCasting && LastCastSpell != "Spear Hand Strike" ||
                     Me.HealthPercent < THSettings.Instance.SUrgentHeal) &&
                    //Me.CurrentEnergy >= 30 &&
                    HasTalent.Contains("Leg Sweep") &&
                    SpellManager.Spells["Leg Sweep"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate { CastSpell("Leg Sweep", UnitAttack); })
                    ),
                //CountFriendlyNearyNeedHealRevive
                new Decorator(
                    ret =>
                    Me.HasAura("Stance of the Wise Serpent") &&
                    Me.CurrentMana > Me.MaxMana*0.07 && // Me.MaxMana * 0.01 &&
                    !Me.Mounted &&
                    Me.Combat &&
                    !CastingorGCDL() &&
                    SpellManager.HasSpell("Revival") &&
                    SpellManager.Spells["Revival"].CooldownTimeLeft.TotalMilliseconds <= MyLatency &&
                    CountFriendlyNearyNeedHealRevive() >= THSettings.Instance.SRevivalUnit,
                    new Action(delegate { CastSpell("Revival", Me); })
                    ),
                //Uplift
                new Decorator(
                    ret =>
                    Me.HasAura("Stance of the Wise Serpent") &&
                    !CastingorGCDL() &&
                    UnitHeal != null && UnitHeal.IsValid &&
                    Me.CurrentChi >= 2 &&
                    SpellManager.HasSpell("Uplift") &&
                    CountUnitUplift() >= THSettings.Instance.SUpliftMinUnit,
                    new Action(delegate { CastSpell("Uplift", Me); })
                    ),
                new Decorator(
                    ret =>
                    !CastingorGCDL() &&
                    UnitHeal != null && UnitHeal.IsValid && UnitHeal.Combat &&
                    UnitHeal.HealthPercent < THSettings.Instance.SZenSphere &&
                    HasTalent.Contains("Zen Sphere") &&
                    Me.CurrentChi >= 2 &&
                    GetUnitZenSphere() && UnitZenSphere.IsValid,
                    new Action(delegate
                                   {
                                       //Logging.Write(LogLevel.Diagnostic, "Call Zen Sphere pls");
                                       CastSpell("Zen Sphere", UnitZenSphere);
                                   })
                    ),
                new Decorator(
                    ret =>
                    UnitHeal != null && UnitHeal.IsValid && UnitHeal.Combat &&
                    Me.IsFacing(UnitHeal) &&
                    !CastingorGCDL() &&
                    UnitHeal.HealthPercent < THSettings.Instance.SChiWave &&
                    HasTalent.Contains("Chi Wave") &&
                    Me.CurrentChi >= 2 &&
                    SpellManager.Spells["Chi Wave"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate
                                   {
                                       //Logging.Write(LogLevel.Diagnostic, "Call Chi Wave pls");
                                       CastSpell("Chi Wave", UnitHeal);
                                   })
                    ),
                //Pre Enveloping Mist
                new Decorator(
                    ret =>
                    Me.HasAura("Stance of the Wise Serpent") &&
                    !Me.IsMoving &&
                    UnitHeal != null && UnitHeal.IsValid && UnitHeal.Combat &&
                    !CastingorGCDL() &&
                    Me.CurrentChi >= 3 &&
                    UnitHeal.HealthPercent < THSettings.Instance.SEnvelopingMist &&
                    !MyAura("Enveloping Mist", UnitHeal) &&
                    (MyAuraTimeLeft("Soothing Mist", UnitHeal) <= MyLatency &&
                     !Me.CurrentMap.IsRaid && !Me.CurrentMap.IsArena) &&
                    SpellManager.HasSpell("Soothing Mist") &&
                    SpellManager.HasSpell("Soothing Mist"),
                    new Action(delegate
                                   {
                                       SpellManager.StopCasting();
                                       CastSpell("Soothing Mist", UnitHeal);
                                   })
                    ),
                new Decorator(
                    ret =>
                    Me.HasAura("Stance of the Wise Serpent") &&
                    !Me.IsMoving &&
                    UnitHeal != null && UnitHeal.IsValid && UnitHeal.Combat &&
                    !CastingorGCDL() &&
                    Me.CurrentChi >= 3 &&
                    UnitHeal.HealthPercent < THSettings.Instance.SEnvelopingMist &&
                    !MyAura("Enveloping Mist", UnitHeal) &&
                    (MyAuraTimeLeft("Soothing Mist", UnitHeal) > MyLatency ||
                     Me.CurrentMap.IsRaid || Me.CurrentMap.IsDungeon) &&
                    SpellManager.HasSpell("Enveloping Mist"),
                    new Action(delegate { CastSpell("Enveloping Mist", UnitHeal); })
                    ),
                //Chi Burst AoE heal                
                new Decorator(
                    ret =>
                    LastCastSpell != "Chi Burst" &&
                    HasTalent.Contains("Chi Burst") &&
                    Me.CurrentChi >= 2 &&
                    GetUnitChiBurstHeal() && UnitChiBurstHeal.IsValid &&
                    !CastingorGCDL(),
                    new Action(delegate
                                   {
                                       CastSpell("Chi Burst", UnitChiBurstHeal);
                                       Logging.Write(LogLevel.Diagnostic, "Chi Burst - Heal");
                                   })
                    ),
                //Pre Surging Mist
                new Decorator(
                    ret =>
                    Me.HasAura("Stance of the Wise Serpent") &&
                    Me.CurrentMana > Me.MaxMana*(0.08 + 0.01) &&
                    !Me.IsMoving &&
                    UnitHeal != null && UnitHeal.IsValid && UnitHeal.Combat &&
                    !CastingorGCDL() &&
                    UnitHeal.HealthPercent < THSettings.Instance.SSurgingMist &&
                    MyAuraTimeLeft("Soothing Mist", UnitHeal) <= MyLatency &&
                    SpellManager.HasSpell("Soothing Mist") &&
                    SpellManager.HasSpell("Surging Mist"),
                    new Action(delegate
                                   {
                                       SpellManager.StopCasting();
                                       CastSpell("Soothing Mist", UnitHeal);
                                   })
                    ),
                new Decorator(
                    ret =>
                    Me.HasAura("Stance of the Wise Serpent") &&
                    Me.CurrentMana > Me.MaxMana*(0.08 + 0.01) &&
                    !Me.IsMoving &&
                    UnitHeal != null && UnitHeal.IsValid && UnitHeal.Combat &&
                    !CastingorGCDL() &&
                    UnitHeal.HealthPercent < THSettings.Instance.SSurgingMist &&
                    //MyAuraTimeLeft("Soothing Mist", UnitHeal) > MyLatency &&
                    SpellManager.HasSpell("Surging Mist"),
                    new Action(delegate { CastSpell("Surging Mist", UnitHeal); })
                    ),
                new Decorator(
                    ret =>
                    !Me.Mounted &&
                    Me.CurrentMana > Me.MaxMana*0.007 &&
                    ValidUnit(UnitAttack) && IsEnemy(UnitAttack) &&
                    Me.IsFacing(UnitAttack) &&
                    !CastingorGCDL() && UnitAttack.IsWithinMeleeRange &&
                    //Me.CurrentEnergy >= 15 &&
                    (UnitAttack.IsPlayer || UnitAttack.Fleeing) &&
                    UnitAttack.MovementInfo.RunSpeed > 3.5 &&
                    DebuffSnare(UnitAttack) <= MyLatency &&
                    SpellManager.HasSpell("Disable"),
                    new Action(delegate
                                   {
                                       //Logging.Write(LogLevel.Diagnostic, "Call Disable pls");
                                       if (!Me.IsFacing(UnitAttack))
                                       {
                                           UnitAttack.Face();
                                       }
                                       CastSpell("Disable", UnitAttack);
                                   })
                    ),
                new Decorator(
                    ret =>
                    !Me.Mounted &&
                    Me.HasAura("Stance of the Wise Serpent") &&
                    Me.Combat &&
                    !CastingorGCDL() &&
                    //UnitHeal != null && UnitHeal.IsValid &&
                    //(UnitHeal.HealthPercent > THSettings.Instance.SPriorityHeal ||
                    // Me.ManaPercent < THSettings.Instance.SUrgentHeal) &&
                    Me.HealthPercent > THSettings.Instance.SUrgentHeal &&
                    Me.HasAura("Mana Tea") && Me.GetAuraByName("Mana Tea").StackCount > 1 &&
                    Me.ManaPercent < THSettings.Instance.SManaTea &&
                    HasGlyph.Contains("Glyph of Mana Tea") &&
                    SpellManager.Spells["Mana Tea"].CooldownTimeLeft.TotalMilliseconds <= MyLatency,
                    new Action(delegate { CastSpell("Mana Tea", Me); })
                    ),
                new Decorator(
                    ret =>
                    !Me.Mounted &&
                    Me.HasAura("Stance of the Wise Serpent") &&
                    Me.CurrentMana > Me.MaxMana*0.055 &&
                    !CastingorGCDL() &&
                    SpellManager.HasSpell("Spinning Crane Kick") &&
                    //Because it generate 1 chi if hit 3 target
                    Me.CurrentChi <= Me.MaxChi - 1 &&
                    Me.CurrentChi >= 2 &&
                    HasTalent.Contains("Rushing Jade Wind") &&
                    SpellManager.Spells["Rushing Jade Wind"].CooldownTimeLeft.TotalMilliseconds <=
                    MyLatency &&
                    CountFriendlyNearyNeedHealSpinningCraneKick(Me, 8) >= THSettings.Instance.SSpinCraneKickHealUnit,
                    new Action(delegate
                                   {
                                       if (!Me.IsSafelyFacing(UnitAttack))
                                       {
                                           UnitAttack.Face();
                                       }
                                       CastSpell("Rushing Jade Wind", Me);
                                   })
                    ),
                //Spinning Crane Kick while Rushing Jade Wind activated
                new Decorator(
                    ret =>
                    Me.HasAura("Stance of the Wise Serpent") &&
                    Me.CurrentMana > Me.MaxMana*0.055 &&
                    !CastingorGCDL() &&
                    SpellManager.HasSpell("Spinning Crane Kick") &&
                    //Because it generate 1 chi if hit 3 target
                    Me.CurrentChi <= Me.MaxChi - 1 &&
                    CountFriendlyNearyNeedHealSpinningCraneKick(Me, 8) >= THSettings.Instance.SSpinCraneKickHealUnit,
                    new Action(delegate
                                   {
                                       //Logging.Write(LogLevel.Diagnostic, "Call Spinning Crane Kick pls");
                                       CastSpell("Spinning Crane Kick", Me);
                                   })
                    ),
                new Decorator(
                    ret =>
                    !Me.Mounted &&
                    THSettings.Instance.SChiAttack &&
                    ValidUnit(UnitAttack) && IsEnemy(UnitAttack) &&
                    Me.IsFacing(UnitAttack) &&
                    !CastingorGCDL() && UnitAttack.IsWithinMeleeRange &&
                    //do something if no mana
                    (Me.CurrentChi >= 2) &&
                    Me.HasAura("Stance of the Wise Serpent") &&
                    (MyAuraTimeLeft("Serpent's Zeal", Me) < 10000 ||
                     MyAuraStackCount("Serpent's Zeal", Me) < 2) &&
                    SpellManager.HasSpell("Teachings of the Monastery"),
                    new Action(delegate
                                   {
                                       //Logging.Write(LogLevel.Diagnostic, "Call Blackout Kick pls");
                                       if (!Me.IsFacing(UnitAttack))
                                       {
                                           UnitAttack.Face();
                                       }
                                       CastSpell("Blackout Kick", UnitAttack);
                                   })
                    ),
                //This spell, it a waste of mana, really
                //new Decorator(
                //    ret =>
                //    THSettings.Instance.SChiAttack &&
                //    Me.ManaPercent > 70 && //Better dont use this on low mana
                //    ValidUnit(UnitAttack) && IsEnemy(UnitAttack) &&
                //    Me.IsFacing(UnitAttack) &&
                //    !CastingorGCDL() && UnitAttack.IsWithinMeleeRange &&
                //    (Me.CurrentChi == Me.MaxChi) &&
                //    (MyAuraTimeLeft("Vital Mists", Me) < 5000 ||
                //     MyAuraStackCount("Vital Mists", Me) < 5) &&
                //    SpellManager.HasSpell("Teachings of the Monastery"),
                //    new Action(delegate
                //                   {
                //                       //Logging.Write(LogLevel.Diagnostic, "Call Blackout Kick pls");
                //                       ConstantFace(UnitAttack);
                //                       CastSpell("Tiger Palm", UnitAttack);
                //                   })
                //    ),


                new Decorator(
                    ret =>
                    !Me.Mounted &&
                    (Me.HasAura("Stance of the Wise Serpent") &&
                     Me.CurrentMana > Me.MaxMana*0.03 ||
                     Me.HasAura("Stance of the Fierce Tiger") &&
                     Me.CurrentEnergy >= 40) &&
                    Me.CurrentChi < THSettings.Instance.SJabChi &&
                    ValidUnit(UnitAttack) && IsEnemy(UnitAttack) &&
                    UnitAttack.IsWithinMeleeRange &&
                    !CastingorGCDL(),
                    new Action(delegate
                                   {
                                       //Logging.Write(LogLevel.Diagnostic, "Call Jab pls");
                                       if (!Me.IsFacing(UnitAttack))
                                       {
                                           UnitAttack.Face();
                                       }
                                       CastSpell("Jab", UnitAttack);
                                   })
                    ),
                //Detox when nothing to do here
                new Decorator(
                    ret =>
                    Me.HasAura("Stance of the Wise Serpent") &&
                    Me.CurrentMana > Me.MaxMana*0.1 && //(0.0458 + 0.02) && // Me.MaxMana * 0.01 &&
                    !Me.Mounted &&
                    !Me.IsMoving &&
                    !CastingorGCDL() &&
                    UnitHeal != null && UnitHeal.IsValid &&
                    UnitHeal.HealthPercent < THSettings.Instance.SSoothingMist &&
                    MyAuraTimeLeft("Soothing Mist", UnitHeal) <= MyLatency,
                    new Action(delegate { CastSpell("Soothing Mist", UnitHeal); })
                    ),
                new Decorator(
                    ret =>
                    !Me.Mounted &&
                    Me.CurrentMana > Me.MaxMana*0.06 &&
                    THSettings.Instance.SBuff &&
                    LastCastSpell != "Legacy of the Emperor" &&
                    !Me.Mounted &&
                    !CastingorGCDL() &&
                    !Me.HasAura("Legacy of the Emperor") &&
                    !Me.HasAura("Blessing of Kings") &&
                    !Me.HasAura("Mark of the Wild") &&
                    //!Me.HasAura("Embrace of the Shale Spider") &&
                    Me.EnergyPercent > 50 &&
                    SpellManager.HasSpell("Legacy of the Emperor"),
                    new Action(delegate { CastSpell("Legacy of the Emperor", Me); })
                    )
                //Make sure facing problem gone forever
                //new Decorator(
                //    ret =>
                //    !Me.Mounted &&
                //    ValidUnit(UnitAttack) &&
                //    IsEnemy(UnitAttack) &&
                //    !Me.IsFacing(UnitAttack) &&
                //    UnitAttack.InLineOfSight &&
                //    !IsOverrideModeOn,
                //    new Action(delegate
                //                   {
                //                       UnitAttack.Face();
                //                       return RunStatus.Failure;
                //                   })
                //    )
                )
                ;
        }

        #endregion
    }
}